package ext.tianma.masschange.datautility;

import java.io.File;

import org.apache.log4j.Logger;

import com.ptc.carambola.rendering.HTMLComponent;
import com.ptc.core.components.descriptor.ComponentDescriptor;
import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.dataUtilities.AttributeDataUtilityHelper;
import com.ptc.core.components.factory.dataUtilities.DefaultDataUtility;
import com.ptc.core.components.factory.dataUtilities.NumberDataUtility;
import com.ptc.core.components.rendering.guicomponents.AttributeGuiComponent;
import com.ptc.core.components.rendering.guicomponents.GUIComponentArray;
import com.ptc.core.components.rendering.guicomponents.TextBox;
import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.ui.resources.ComponentMode;

import ext.tianma.util.PropertiesUtil;
import wt.httpgw.URLFactory;
import wt.log4j.LogR;
import wt.util.WTException;

public class ControlDocCheckHrefDataUtility extends NumberDataUtility {
	private static final Logger log = LogR.getLogger(ControlDocCheckHrefDataUtility.class.getName());

	// private Locale locale;
	@Override
	public Object getDataValue(String s, Object obj, ModelContext modelcontext) throws WTException {
		System.out.println("===========start into ControlDocCheckHref=======" + "s:" + s);

		Object localObject = "";
		String baseUrl = "";
		URLFactory urlfactory;

		if ("cn.tianma.checkurl".equals(s)) {

			if (modelcontext.getDescriptorMode() == ComponentMode.CREATE) {
				GUIComponentArray componentArray = new GUIComponentArray();
				TypeIdentifier typeidentifier = modelcontext.getTypeIdentifier();
				System.out.println(
						"===========start into ControlDocCheckHref name:=======" + typeidentifier.getTypename());
				// 受控PDF图纸
				if ((typeidentifier.getTypename().indexOf("cn.tianma.ControlDocument") != -1)) {

					ComponentDescriptor componentdescriptor = modelcontext.getDescriptor();
					String s1 = componentdescriptor.getModelAttributeString();
					if (s1 == null || s1.length() == 0)
						s1 = s;
					String namevalue = AttributeDataUtilityHelper.getValue(modelcontext, s);
					TextBox box = new TextBox();
					box.setInputType("hidden");
					box.setId(s1);
					box.setRequired(AttributeDataUtilityHelper.isInputRequired(modelcontext));
					box.setColumnName(AttributeDataUtilityHelper.getColumnName(s1, obj, modelcontext));
					box.setValue(namevalue);
					componentArray.addGUIComponent(box);
					// if(s.indexOf("LocationZH")!=-1){
					// 从配置文件读取模板链接
					String configFilePath = "codebase" + File.separator + "ext" + File.separator + "tianma"
							+ File.separator + "cfg" + File.separator + "TianmaPro.properties";

					PropertiesUtil ut = new PropertiesUtil(configFilePath);
					String url = ut.getValueByKey("controlDoc.url");

					try {
						urlfactory = new URLFactory();
						baseUrl = urlfactory.getBaseHREF();

					} catch (Exception e) {
						// e.printStackTrace();
						log.error(e.getMessage(), e);
					}
					//url = baseUrl + url;
					url = baseUrl + "netmarkets/jsp/padShow/jump.jsp";
					// String u = "<a href=\"" + baseUrl + "app/#ptc1/tcomp/infoPage.jsp\" >" +
					// "模板下载" + "</a>";
					String u = "<a href=\"" + url + "\" target='_blank' >" + "模板下载" + "</a>";
					// String u = "<a href=\"javascript:window.open('" + url
					// +
					// "','_blank','width=750,height=470,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes');\">"
					// +"模板下载" + "</a>";

					/*
					 * String u0 = "<input type=\"button\" name=\"submitButton\" value=\'" + "模板下载"
					 * + "\' onclick=\"var thisurl=location.href;var url='" + url +
					 * "window.open(url,'_blank','width=750,height=470,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes') \"/>"
					 * ;
					 */
					/*
					 * String u = "<input type=\"button\" name=\"submitButton\" value=\'" + "模板下载" +
					 * "\' onclick=\"var thisurl=location.href;var url='" + url +
					 * "';window.open(url,'_blank','width=750,height=470,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes')\">";
					 */
					System.out.println("====u:" + u);
					componentArray.addGUIComponent(new HTMLComponent(u));
					// }

					localObject = componentArray;
				} else {
					localObject = componentArray;
				}
			} else {
				localObject = super.getDataValue(s, obj, modelcontext);
			}
		}
		return localObject;
	}

}
